package com.yswy.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.yswy.app.dto.AppUserUpdateReqVO;
import com.yswy.app.dto.LoginUserDto;
import com.yswy.app.dto.UserMoneyLogVo;
import com.yswy.app.res.SecurityParameter;
import com.yswy.app.service.*;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.UserLockUtils;
import com.yswy.domain.entity.AppUser;
import com.yswy.domain.entity.UserWxOpen;
import com.yswy.domain.entity.WxProgramConfig;
import com.yswy.domain.vo.dto.TgDeviceHd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import static com.yswy.domain.common.util.TokenUtils.*;

@RestController
@RequestMapping("/sso")
@Api(description = "登录相关")
@Slf4j
public class SsoController {
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private IpPlaceService ipPlaceService;
    @Autowired
    private UserMoneyLogService userMoneyLogService;
    @Autowired
    private WxProgramConfigService wxProgramConfigService ;
    @Autowired
    private TgCallBackHdIdService tgCallBackHdIdService ;
    /** 发送验证码 **/
    @GetMapping("/sendCode")
    @ApiOperation(value = "发送验证码")
    public ResultModel userPage(@RequestParam(value = "phone") String phone) {
        return appUserService.sendCode(phone);
    }

    /** 登录 **/
    @PostMapping("/login")
    @ApiOperation(value = "小程序登录")
//    @SecurityParameter()
    public ResultModel login(@RequestBody LoginUserDto loginUserDto ,HttpServletRequest request ) {

        synchronized (UserLockUtils.getLock("loginPhone:"+loginUserDto.getCode())){
            log.info(loginUserDto.toString());
            log.info(loginUserDto.getQueryString());
            ResultModel resultModel = appUserService.loginProgram(loginUserDto ,getIP(request)  , getChannle(request),getVersion(request));
            return resultModel ;
        }
    }
    /** 登录 **/
    @PostMapping("/loginApp")
    @ApiOperation(value = "app登录")
    @SecurityParameter()
    public ResultModel loginApp(@RequestBody LoginUserDto loginUserDto ,HttpServletRequest request ) {

        synchronized (UserLockUtils.getLock("loginPhone:"+loginUserDto.getCode())){
            log.info(loginUserDto.toString());
            log.info(loginUserDto.getQueryString());
            ResultModel resultModel = appUserService.loginApp(loginUserDto ,getIP(request)  , getChannle(request),getVersion(request));
            return resultModel ;
        }
    }

//    /** 获取访问页面 **/
//    @PostMapping("/getIpPlaceStatus")
//    @ApiOperation(value = "获取访问页面")
//    public ResultModel getIpPlaceStatus(HttpServletRequest request ) {
//        AppUser appUser = getUser(request);
//        String ip = getIP(request);
//        if (appUser != null){
//            if (StringUtils.isNotEmpty(appUser.getCreateIp())){
//                ip = appUser.getCreateIp();
//            }
//        }
//        Boolean is = ipPlaceService.getIpPlaceStatus(ip);
//        return ResultModel.ok(is) ;
//    }

    /** 获取用户金币记录 **/
    @PostMapping("/listUserMoneyLog")
    @ApiOperation(value = "获取用户金币记录")
//    @SecurityParameter()
    public ResultModel listUserMoneyLog(@RequestBody UserMoneyLogVo userMoneyLogVo , HttpServletRequest request ) {
        return ResultModel.ok(userMoneyLogService.listUserMoneyLog(getUserId(request) , userMoneyLogVo));
    }


    /** 获取用户信息 **/
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public ResultModel getUserInfo(HttpServletRequest request ) {
        return appUserService.getUserInfo(getUserId(request));
    }

    /** 获取小程序链接 **/
    @PostMapping("/getOpenlink")
    @ApiOperation(value = "获取小程序链接")
    public ResultModel getOpenlink(HttpServletRequest request,@RequestBody  Map<String, String> queryMap ) {
        synchronized (UserLockUtils.getLock("getOpenlink")){
            log.info(JSONObject.toJSONString(queryMap));
            //推呀回传
//            https://imgser.zyykj168.com/jmhdh5/index.html#/pages/index/index?
//            goodsId=1
//            &channle=ty-001
//            &type=2
//            &ua=__tuia-ua__
//            &a_oId=__tuia-oId__
//            &imeiMd5=__tuia-imeiMd5__
//            &idfaMd5=__tuia-idfaMd5__
//            &oaidMd5=__tuia-oaidMd5__
//            &deviceMd5=__tuia-deviceMd5__
//            &ip=__tuia-ip__

            TgDeviceHd tgDeviceHd = new TgDeviceHd( queryMap.get("a_oId"), getIP(request), queryMap.get("ua")
                    , queryMap.get("imeiMd5"), queryMap.get("idfaMd5"), queryMap.get("oaidMd5"), queryMap.get("deviceMd5"));
            tgCallBackHdIdService.addTgCallBackHdId(queryMap.get("channle") ,tgDeviceHd , Integer.parseInt(queryMap.get("type")) );
//            https://imgser.zyykj168.com/jmhdh5/index.html?#/pages/index/download
//            http://imgser.zyykj168.com/jmhdh5/index.html?#/pages/index/download
            return ResultModel.ok(wxProgramConfigService.getOpenlink(getIP(request) ,queryMap ));
        }
    }

    /** 添加小程序链接 **/
    @GetMapping("/addOpenlink")
    @ApiOperation(value = "添加小程序链接")
    public ResultModel addOpenlink(HttpServletRequest request  , @RequestParam(value = "num") Integer num) {
        wxProgramConfigService.addOpenlink(num);
        return ResultModel.ok();
    }




//    @GetMapping("/shielding")
//    @ApiOperation(value = "账号注销")
//    public ResultModel shielding(HttpServletRequest request ) {
//        appUserService.shielding(getUserId(request));
//        return ResultModel.ok();
//    }
//    @PostMapping("/updateInfo")
//    @ApiOperation(value = "修改用户信息")
//    public ResultModel updateInfo(HttpServletRequest request , @RequestBody() AppUserUpdateReqVO req) {
//        appUserService.updateInfo(req ,getUserId(request) );
//        return ResultModel.ok() ;
//    }
//    /** 修改用户性别 **/
//    @GetMapping("/updateUserSex")
//    @ApiOperation(value = "修改用户性别")
//    public ResultModel updateUserSex(@RequestParam(value = "sex") Integer sex,HttpServletRequest request) {
//        appUserService.updateUserSex(sex , getUserId(request));
//        return ResultModel.ok();
//    }
//    @GetMapping("/updateUserDevice")
//    @ApiOperation(value = "修改用户数据")
//    public ResultModel updateUserDevice(@RequestParam(value = "pwd") String pwd) {
//        if (pwd.equals("ai123156")){
//            appUserService.updateUserDevice();
//        }
//        return ResultModel.ok() ;
//    }
//    //    https://tlsp.changyou.com/tl/goddess/wxVote
//    @PostMapping("/testAes1")
//    @ApiOperation(value = "加密测试1")
//    @SecurityParameter()
//    public ResultModel testAes1(@RequestBody LoginUserDto loginUserDto ,HttpServletRequest request ) {
//        System.out.println(loginUserDto.toString());
//        String string = "haode" ;
//        return ResultModel.ok(string);
//    }

}
